package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealNameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    ApAuthorFeign apAuthorFeign;

    @Autowired
    WemediaFeign wemediaFeign;

    @Autowired
    ApUserMapper apUserMapper;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null){
            return errorParam;
        }
        QueryWrapper<ApUserRealname> wrapper = new QueryWrapper<>();
        if (dto.getStatus() != null){
            wrapper.lambda().like(ApUserRealname::getStatus, dto.getStatus());
        }
        return getPageData(dto,wrapper);
    }


    @Override
    @GlobalTransactional
    public ResponseResult passAndFail(AuthDto dto) {
        if (dto == null || dto.getId() == null)return errorParam;
        Short status = dto.getStatus();
        ApUserRealname realname = new ApUserRealname();
        realname.setId(dto.getId());
        realname.setStatus(status);
        if (status == 2 ) {
            realname.setReason(dto.getMsg());
            updateById(realname);
            return ResponseResult.okResult(AppHttpCodeEnum.FAILED);
        }else if (status == 9){
            updateById(realname);
        }

        //获取用户信息
        ApUserRealname userRealname = getById(dto.getId());
        ApUser apUser = this.apUserMapper.selectById(userRealname.getUserId());
        if (apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST);
        }

        //创建保存自媒体账号
        WmUser wmUser = new WmUser();
        wmUser.setSalt(apUser.getSalt());
        wmUser.setName(apUser.getName());
        wmUser.setApUserId(apUser.getId());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setStatus(dto.getStatus().intValue());
        wmUser.setCreatedTime(new Date());
        wmUser.setPhone(apUser.getPhone());
        ResponseResult save = wemediaFeign.save(wmUser);

        Integer wmUserId = (Integer) save.getData();

        //创建保存作者账号
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setType(2);
        author.setCreatedTime(new Date());
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUserId);
        ResponseResult save1 = apAuthorFeign.save(author);

        //需重新向自媒体表里面添加作者表id
        wmUser.setId(wmUserId);
        wmUser.setApAuthorId((Integer) save1.getData());
        wemediaFeign.update(wmUser);

        apUser.setFlag((short) 1);
        this.apUserMapper.updateById(apUser);
     /*   int i=1/0;*/
        return success;
    }

}
